package com.whs.demo.ui;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
/**
 * 蒙版
 * @Description:   
 * @author whs   
 * @date 2015-11-24 下午5:05:56 
 * @version V1.0 
 *
 */
public class Mask
    extends Actor {

    public final  static float DEFFAULT_OPACITY = 0.7F;
    public final static Vector2 SCREEN_SIZE = new Vector2(480.0F, 800.0F);
    private float opacity;
    private ShapeRenderer shapeRenderer;
    private OrthographicCamera camera;

    public Mask() {
        this(DEFFAULT_OPACITY);
    }

    public Mask(float opacity) {
        setOpacity(opacity);
        setColor(Color.BLACK);
    }

    public Mask(float opacity, Color color) {
        setOpacity(opacity);
        setColor(color);
    }

    public void addAction(Action action) {
        super.addAction(action);
    }

    protected void begin(Batch batch, float parentAlpha) {
        batch.end();
        Gdx.gl.glEnable(3042);
        Gdx.gl.glBlendFunc(770, 771);

        shapeRenderer.setProjectionMatrix(camera.combined);
        shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
        Color color = getColor();
        shapeRenderer.setColor(color.r, color.g, color.b, parentAlpha * color.a * opacity);
    }

    public void draw(Batch batch, float parentAlpha) {
        begin(batch, parentAlpha);
        mask(0.0F, 0.0F, 480F, 800F);
        end(batch);
    }

    protected void end(Batch batch) {
        shapeRenderer.end();
        Gdx.gl.glDisable(3042);
        batch.begin();
    }

    public void fadOut() {
        fadeOut(0.5F);
    }

    public void fadeIn() {
        fadeIn(0.5F);
    }

    public void fadeIn(float deltaTime) {
        clearActions();
        setVisible(true);
        getColor().a = 0.0F;
        addAction(Actions.fadeIn(deltaTime));
    }

    public void fadeOut(float deltaTime) {
        clearActions();
        addAction(Actions.sequence(Actions.fadeOut(deltaTime), Actions.visible(false)));
    }

    public float getOpacity() {
        return opacity;
    }

    protected void mask(float x, float y, float width, float height) {
        shapeRenderer.rect(x, y, width, height);
    }

    public void setOpacity(float f) {
        opacity = f;
    }

}
